package hr.fer.apr.ht41337.lab2.funkcije;

import hr.fer.apr.ht41337.lab2.Tocka;

import java.io.*;

public class F3 implements FunkcijaOgranicena {
	
	private double[] p; 

	public F3(File f) throws IOException
	{
		BufferedReader br = new BufferedReader(new FileReader(f));
		String line = br.readLine();
		String[] pars = line.split(" ");
		if (pars.length != 5)
			throw new IOException("krivo zadani parametri u datoteci " + f.getName() );
		p = new double[pars.length];
		for (int i = 0; i < pars.length; i++)
			p[i] = Double.parseDouble(pars[i]);
	}
	
	@Override
	public double getXd(int i) {
		if (i < 0 || i > 4)
			throw new IllegalArgumentException("pogresan index: i=" + i);
		return -100;
	}

	@Override
	public double getXg(int i) {
		if (i < 0 || i > 4)
			throw new IllegalArgumentException("pogresan index: i=" + i);
		return 100;
	}

	@Override
	public boolean zadovoljavaImplicitnaOgranicenja(Tocka t) {
		if ((t.getX(0) - t.getX(1) <= 0) && (t.getX(0) - 2 <= 0))
			return true;
		return false;
	}
	
	@Override
	public boolean zadovoljavaEksplicitnaOgranicenja(Tocka t){
		if (t.dimenzija != 5)
			throw new IllegalArgumentException("tocka pogresnih dimenzija");
		for (int i = 0; i < getDimenzija(); i++)
			if (t.getX(i) < getXd(i) || t.getX(i) > getXg(i))
				return false;
		return true;
	}

	@Override
	public int getDimenzija() {
		return 5;
	}

	@Override
	public double getValue(Tocka t) {
		double value = 0;
		if (t.dimenzija != 5)
			throw new IllegalArgumentException("tocka pogresnih dimenzija");
		for (int i = 0; i < getDimenzija(); i++)
			value += Math.pow(t.getX(i) - p[i], 2);
		return value;
	}

}
